package org.testis.terminalapplicationmodule.services;

import org.testis.data.TestStepException;
import org.testis.data.TestStepResultType;
import org.testis.logging.AtofLogLevel;
import org.testis.logging.LogSenderService;
import org.testis.services.AtofModuleAction;
import org.testis.services.AtofModuleService;
import org.testis.terminalapplicationmodule.data.TerminalApplicationModuleDataObject;


/**
 * Defines an action that does exit code verification of a terminall application
 * @author nurhat
 */
public class ExitCodeVerificationAction extends AtofModuleAction
{

    @Override
    public boolean operateRequest(AtofModuleService request)
    {
        TerminalApplicationModuleDataObject dataObj =
            (TerminalApplicationModuleDataObject) request.getDataObj();
        String expectedResult = dataObj.getExpectedExitCode();
        if (expectedResult == null) {
            throw new TestStepException(TestStepResultType.MISTAKE,
                                        "Expected exit code result should not be null...");
        }
        if (expectedResult.equals(dataObj.getExitCode())) {
            LogSenderService.putMessageOnQueue(
                "TerminalAppExitCodeVerificationAction",
                AtofLogLevel.INFO,
                "Expected exit code result was verified. ExitCode:" + dataObj.
                getExitCode() + " Expected ExitCode:" +
                expectedResult,
                dataObj.getMessageId());
            return true;
        }

        LogSenderService.putMessageOnQueue(
            "TerminalAppExitCodeVerificationAction",
            AtofLogLevel.ERROR,
            "Expected exit code result was not verified. ExitCode:" + dataObj.
            getExitCode() + " Expected ExitCode:" +
            expectedResult,
            dataObj.getMessageId());
        return false;

    }
}
